package co.com.inventarios.persistencia;

import java.util.List;
import java.util.logging.Level;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import co.com.inventarios.modelo.MngmcnId;
import co.com.inventarios.modelo.Mngmcnsn;
import co.com.inventarios.modelo.MngmcnsnId;
import co.com.inventarios.modelo.Unimed;

public class QueryMngmcnsnDAO {

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	public List<Mngmcnsn> findByPropertyAll(String propertyName, Object value,
			String espresion) {

		EntityManagerHelper.log("finding Mngmcnsn instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngmcnsn model where model."
					+ propertyName
					+ " LIKE '%"
					+ value
					+ "%'  "
					+ espresion
					+ " ";
			Query query = getEntityManager().createQuery(queryString);

			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Mngmcnsn> findBy_all(String propertyName) {

		EntityManagerHelper.log("finding Mngmcnsn instance with property: "
				+ propertyName + ", value: ", Level.INFO, null);

		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngmcnsn model "
					+ propertyName + " ";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Mngmcnsn> findBy_all_Ing(MngmcnId id) {

		EntityManagerHelper.log("finding Mngmcnsn instance with property: "
				+ " " + ", value: ", Level.INFO, null);

		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngmcnsn model where model.id.msnempresa ='"
					+ id.getMcnempresa()
					+ "' and model.id.msnclase ='"
					+ id.getMcnclase()
					+ "' and model.id.msntipodoc = '"
					+ id.getMcntipodoc()
					+ "' and model.id.msnnumedoc =  "
					+ id.getMcnnumedoc()
					+ " and model.id.msnreg ="
					+ id.getMcnreg() + " order by model.id.msnserial  ";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public Mngmcnsn findByProperty(String propertyName) {
		// TODO Auto-generated method stub
		EntityManagerHelper.log("finding Mngmcnsn instance with property: "
				+ propertyName + ", value: ", Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngmcnsn model "
					+ propertyName + " ";
			Query query = getEntityManager().createQuery(queryString);

			Mngmcnsn result = new Mngmcnsn();
			if (query.getResultList().toArray().length > 0) {
				result = (Mngmcnsn) query.getSingleResult();
			} else {
				result = null;
			}

			return result;

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public Mngmcnsn findByPropertyActivo(String propertyName, Object value) {
		// TODO Auto-generated method stub
		EntityManagerHelper.log("finding Mngmcnsn instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngmcnsn model where model.msnestado = 1 AND model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);

			Mngmcnsn result = new Mngmcnsn();

			if (query.getResultList().size() > 0) {
				result = (Mngmcnsn) query.getSingleResult();
			} else {
				result = null;
			}

			return result;

		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Mngmcnsn> findByPropertyActivoLista(String propertyName) {
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngmcnsn model where model.msnestado = 1 "
					+ propertyName + " ";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

}
